<?php

namespace App\ApiBackend\Department;

use App\Http\Controllers\ApiBaseController;
use Illuminate\Support\Facades\DB;


/**
 *  分页
 */
class Page extends ApiBaseController
{

  /**
   * 参数检查
   */
  protected function check(): void
  {
  }

  /**
   * 业务主体
   */
  protected function service(): void
  {
    $type = request('type');

    //基础查询构造器
    $selectFields = [
      "aa.*",
    ];
    $categoryOneList = DB::table('admin_department as aa')
      ->select($selectFields)
      ->where('aa.pid', 0)
      ->orderBy("aa.orderNo", "desc")
      ->get();

    if ($type != 'select') {
      foreach ($categoryOneList as $itemOne) {
        $categoryTwoList = DB::table('admin_department as aa')
          ->select($selectFields)
          ->where('aa.pid', $itemOne->id)
          ->orderBy("aa.orderNo", "desc")
          ->get();
        $itemOne->children = $categoryTwoList;
      }
    }

    //数据处理
    if ($type === 'select') {
      $categoryOneList = array_merge([
        [
          'id' => 0,
          'deptName' => '无上级',
        ]
      ], $categoryOneList->toArray());
    }

    //返回结果
    $this->result["data"]["list"] = $categoryOneList;
    return;
  }
}
